<template>
  <div class="giftModule publicHCenter">
    <div class="giftBox" :class="{ show: isShowAni }">
      <div class="giftListWrap">
        <swipe ref="sw" class="gift-swipe" :auto="0" @change="change">
          <swipe-item class="page" :key="index" v-for="(group, index) in giftList_format">
            <div
              v-for="gift in group"
              class="item publicHvCenter"
              v-bind:key="gift.giftId"
              v-tap="{ methods: chooseGiftType, giftId: gift.giftId }"
            >
              <div class="price publicHvCenter">{{ gift.price }}<img src="@/assets/image/icon/diamond.png" class="unit" /></div>
              <img class="img" v-bind:src="gift.giftImg" :class="{ active: gift.giftId === currentGiftType }" />
              <div class="name">{{ gift.name }}</div>
            </div>
          </swipe-item>
        </swipe>
      </div>
      <div class="lineWrap">
        <div class="common-line"></div>
        <div class="active-line">
          <div
            :key="index"
            v-for="(item, index) in giftList_format"
            class="item"
            :style="{ width: 100 / giftList_format.length + '%' }"
            :class="[{ active: index == currentSwIndex }]"
          ></div>
        </div>
      </div>
      <div class="payWrap">
        <div class="remainDiamonds publicHvCenter">
          <img src="https://static.hudongmiao.com/joymewH5/img/gift/diamond.png" class="diamondImg" />
          <span class="remain">{{ money }}</span>
        </div>
        <div class="sendBtn publicHvCenter" v-tap="{ methods: send }">发送礼物</div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
import { Swipe, SwipeItem } from 'vue-swipe';
import { sendGift } from '@/api/index/index';
import { sendGiftMessage } from '@/api/chat/index';
import { POPUP_MODULE } from '@/assets/constant/index';
import { timeoutTask } from '../../utils/index';

export default {
  name: 'giftPopup',
  computed: {
    ...mapState({
      giftList_format: (state) => state.live.giftList_format,
      giftList: (state) => state.live.giftList,
      currentGiftType: (state) => state.app.currentGiftType,
      money: (state) => state.user.money,
      freeSendGift: (state) => state.user.freeSendGift,
    }),
  },
  created() {},
  components: {
    Swipe,
    SwipeItem,
  },
  data() {
    return {
      currentSwIndex: 0,
      isShowAni: false, // 控制礼物box弹出动画
    };
  },
  mounted() {
    // 执行弹出动画
    this.showAniStart();
    console.log(this.giftList_format);
    console.log(this.giftList);
  },
  methods: {
    ...mapMutations({
      setCurrentGiftType: 'app/setCurrentGiftType',
      setUserInfo: 'user/setUserInfo',
      togglePopup: 'app/togglePopup',
      setOrigin: 'app/setOrigin',
    }),
    change(cur) {
      this.currentSwIndex = cur;
    },
    showAniStart() {
      timeoutTask(() => {
        this.isShowAni = true;
      }, 100);
    },
    hideAniStart() {
      this.isShowAni = false;
    },
    chooseGiftType(g) {
      this.setCurrentGiftType(g.giftId);
    },
    validateSend() {
      let flag = true;
      const choosedGift = this.giftList.find((item) => item.giftconst === this.currentGiftType);
      console.log('***validateSend freeSendGift***');
      console.log(this.freeSendGift);
      if (this.freeSendGift) {
        flag = true;
      } else if (choosedGift.giftprice > this.money) {
        this.$toast.center('余额不足,请充值!');
        flag = false;
        this.setOrigin('sendGift');
        this.togglePopup();
        timeoutTask(() => {
          this.togglePopup(POPUP_MODULE.rechargeModule.key);
        }, 200);
      }
      return flag;
    },
    sendBroadcast() {
      sendGiftMessage();
    },
    send() {
      if (!this.validateSend()) {
        return;
      }

      this.$loading('发送中...');
      sendGift()
        .then((res1) => {
          this.$loading.close();
          // 更新 用户余额
          this.setUserInfo({
            money: res1.result.balance,
          });
          this.$toast.center('发送成功!');
          this.togglePopup();
          return this.sendBroadcast(); // 发送礼物的广播
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
<style lang="less" scoped>
@import 'vue-swipe/dist/vue-swipe.css';

.giftModule {
  position: absolute;
  width: 100%;
  height: 708px;
  bottom: 0;
  overflow: hidden;
  .giftBox {
    width: 750px;
    height: 708px;
    background-image: url(https://static.hudongmiao.com/joymewH5/img/popupArea/box.png);
    background-size: 100% 100%;
    background-repeat: no-repeat;
    transition: all 0.2s linear;
    position: absolute;
    top: 708px;
    background-position-y: 90px;
    &.show {
      transform: translateY(-708px);
    }
    .giftListWrap {
      position: absolute;
      width: 100%;
      height: 380px;
      top: 157px;
      .gift-swipe {
        width: 100%;
        height: 100%;
        position: absolute;
        .page {
          width: 100%;
          height: 100%;
          position: absolute;
          padding: 0 7.5px;
          .item {
            width: 180px;
            height: 180px;
            position: relative;
            float: left;
            flex-direction: column;
            .price {
              position: absolute;
              top: 0px;
              color: #884c41;
              left: 112.5px;
              font-size: 16px;
              background-image: url(https://static.hudongmiao.com/joymewH5/img/gift/priceBox.png);
              background-size: 100% 100%;
              white-space: nowrap;
              padding: 5px;
              .unit {
                width: 28px;
                height: 21px;
                margin-left: 6px;
              }
            }
            .img {
              position: absolute;
              top: 30px;
              width: 100px;
              height: 100px;
              &.active {
                animation-name: bounceIn;
                animation-duration: 2.5s;
                animation-iteration-count: infinite;
                animation-delay: 0;
              }
            }
            .name {
              font-size: 24px;
              color: #fff;
              position: absolute;
              bottom: 7px;
            }
          }
        }
      }
    }
    .lineWrap {
      position: absolute;
      width: 100%;
      height: 4px;
      top: 565px;
      .common-line {
        width: 680px;
        height: 100%;
        background-color: rgba(255, 255, 255, 0.3);
        border-radius: 2px;
        position: absolute;
        left: 43px;
      }
      .active-line {
        position: absolute;
        top: 0;
        left: 43px;
        height: 100%;
        width: 680px;
        .item {
          height: 100%;
          float: left;

          &.active {
            background-color: rgb(80, 0, 1);
            background-image: linear-gradient(#fbb905, #fbb905), linear-gradient(#ffffff, #ffffff);
            background-blend-mode: normal, normal;
            border-radius: 2px;
          }
        }
      }
    }
    .payWrap {
      width: 100%;
      height: 109px;
      position: absolute;
      bottom: 0;
      padding: 0 37px 0 43px;
      display: flex;
      justify-content: space-between;
      .remainDiamonds {
        width: 217px;
        height: 80px;
        border-radius: 40px;
        border: solid 1px #fee857;
        .diamondImg {
          width: 32px;
          height: 25px;
        }
        .remain {
          font-size: 28px;
          letter-spacing: 1px;
          color: #ffffff;
          margin-left: 14px;
        }
      }
      .sendBtn {
        width: 260px;
        height: 80px;
        font-size: 34px;
        letter-spacing: 3px;
        color: #a24935;
        background-image: url(https://static.hudongmiao.com/joymewH5/img/gift/btnBg.png);
        background-size: 100% 100%;
      }
    }
  }
  @keyframes bounceIn {
    0%,
    100%,
    20%,
    40%,
    60%,
    80% {
      -webkit-transition-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
      transition-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
    }

    0% {
      opacity: 1;
      transform: scale3d(0.9, 0.9, 0.9);
    }

    20% {
      transform: scale3d(1.2, 1.2, 1.2);
    }

    40% {
      transform: scale3d(0.9, 0.9, 0.9);
    }

    60% {
      transform: scale3d(1.2, 1.2, 1.2);
    }

    80% {
      transform: scale3d(0.9, 0.9, 0.9);
    }

    100% {
      transform: scale3d(1, 1, 1);
    }
  }
}
</style>
